# Maintainer: Michał Łukowski <michal.lukowski@gmail.com>

_realname=abseil-cpp
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=20220623.1
pkgrel=2
pkgdesc="Abseil Common Libraries (C++) from Google (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://abseil.io"
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("https://github.com/abseil/abseil-cpp/archive/$pkgver/$_realname-$pkgver.tar.gz"
        https://github.com/abseil/abseil-cpp/commit/8bf19a07e068b17c2bc2a32aed0ba6d48a80411f.patch
        "0001-abseil-Fix-compiler-warnings.patch"
        "0002-abseil-Remove-librt-library.patch"
        "0004-abseil-Propagate-cxx-std-17.patch")
sha256sums=('91ac87d30cc6d79f9ab974c51874a704de9c2647c40f6932597329a282217ba8'
            'eb79e044979be9f08d8e62e4c10327091a74b353daac89497c998aceac2052e9'
            '30fb7526d1c89f722dd0ce0c467e034370140fbf07a1ad4f91df301c7f442551'
            '8946282db6a225b385729deb345b57e3ee75f4e859dbbc49ab7bc5604d71192f'
            '52cbf35dace3296dbe0f72acc2235cf009c0fffce8cf0fb455a99a71e5f25ab2')

prepare() {
  cd "${_realname}-${pkgver}"

  patch -Np1 -i "${srcdir}/8bf19a07e068b17c2bc2a32aed0ba6d48a80411f.patch"
  patch -Np1 -i "${srcdir}/0001-abseil-Fix-compiler-warnings.patch"
  patch -Np1 -i "${srcdir}/0002-abseil-Remove-librt-library.patch"
  patch -Np1 -i "${srcdir}/0004-abseil-Propagate-cxx-std-17.patch"
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  # shared build with clang fails, see
  # https://github.com/msys2/MINGW-packages/pull/8653#issuecomment-999640833

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS="$( [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] && echo OFF || echo ON )" \
      -DCMAKE_CXX_STANDARD=17 \
      -DABSL_PROPAGATE_CXX_STD=ON \
      ../${_realname}-${pkgver}

  cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" cmake --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
